/*
 * WorkQueue.h
 *
 *	Author: stravant
 *	Description:
 *		A work thread-safe work queue. An efficient implementation for a
 *		queue which has both a reader and writers. A "swap" command moves
 *		work posted by the writers to the reader queue.
 *		Is safe for access by 1 reader and N writers
 */

#ifndef WORKQUEUE_H_
#define WORKQUEUE_H_

#include <boost/thread/mutex.hpp>
#include "../core/Include.h"

#include <deque>

namespace tbt {

template <class T>
class WorkQueue {
public:
	WorkQueue();
	~WorkQueue();

	//push data into the queue.
	void push(T a);

	//pop data out of the queue.
	T pop();

	//current element
	T& next() const;

	//move work to the read queue. Returns true if there is
	//any work in the read queue after swaping, false otherwise.
	bool swap();

	//returns whether or not the read-queue is empty.
	bool empty() const;
private:
	class ListNode {
	public:
		ListNode(T value): Value(value), Next(nullptr) {}
		~ListNode() {delete Next; }
		T Value;
		ListNode* Next;
	};

	ListNode* mReadQueueHead;
	ListNode* mReadQueueTail;
	ListNode* mWriteQueueHead;
	ListNode* mWriteQueueTail;
	boost::mutex mWriteLock;
	std::deque<T> a;
};

}

#endif /* WORKQUEUE_H_ */
